package com.lkwlw.tplms.system.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lkwlw.tplms.model.CustomerinfoExample;
import com.lkwlw.tplms.model.Pager;
import com.lkwlw.tplms.system.dao.mapper.AdmininfoMapper;
import com.lkwlw.tplms.system.dao.model.Admininfo;
import com.lkwlw.tplms.system.dao.model.AdmininfoExample;
import com.opensymphony.xwork2.ActionSupport;

public class QueryUserAction extends ActionSupport {
	private static final long serialVersionUID = 5782160233262833479L;
	
	private AdmininfoMapper admininfoMapper;
	
	private List<Admininfo> admininfos;
	private Pager pager;	
	private int page = 1;
	
	public String execute(){
		AdmininfoExample example = new AdmininfoExample();
		example.setOrderByClause("username asc");
		getPagination(example);		
		return SUCCESS;
	}
	
	private void getPagination(AdmininfoExample example){
		int pageSize=10;
		pager.setCurrentPage(page);
		pager.setPageSize(pageSize);
		
		// 获得总记录数
		int totalSize = admininfoMapper.countByExample(example);
		pager.setTotalCount(totalSize);
		Map<String, Integer> limitParam = new HashMap<String, Integer>();
		limitParam.put("offset", pager.getStart());
		limitParam.put("rows", pager.getPageSize());
		
		admininfos = null;		
		admininfos = admininfoMapper.selectByExample(limitParam,example);
		pager.setItems(admininfos);
		pager.setPaginationModel(getPagerStr(totalSize));		
	}
	/**
	 * 组织分页导航栏
	 * @return
	 */
	public String  getPagerStr(int totalSize) {
		
		StringBuilder sb=new StringBuilder();
		
		int pageCount=pager.getTotalPage();//总页数
		int a_total=10;      //分页条中有多少个超链接
		
		sb.append("<table><tr><td align='center'class='wordblue'>共" + pager.getTotalCount()+"条数据&nbsp;&nbsp;页次");
		sb.append(pager.getCurrentPage()+"/" + (totalSize == 0 ? 1 : pageCount) + "页&nbsp;&nbsp;</td>");
		//实际应用中修改上面第三个参数即可

		int a_padding=(int)Math.ceil(a_total/2);  //中间的那个超链接距离边缘链接的间隔a的个数 例如：共11个分页 那么这个就是5

		if(pageCount - pager.getCurrentPage() <= a_padding && pager.getCurrentPage() > a_padding + 1) {
			a_padding=a_total-(pageCount-pager.getCurrentPage());
		}

		int start=pager.getCurrentPage()-a_padding,
		end=start+a_total;
		

		if(pager.getCurrentPage() > 1){
			sb.append("<td class='wordblue'><a href=system/queryUser?page=1>首页</a>&nbsp;&nbsp;</td>");
			sb.append("<td ><div class='prepage wordblue'><a href=system/queryUser?page="+(pager.getCurrentPage()-1)+">上一页</a></div></td>");
		}
		sb.append("<td ><div class='pagelist'>");
		if (totalSize > 0) {
			for (int i = start; i <=end; i++) {
				if(i<=0){
					end+=Math.abs(i);
					i=1;	
				}
				sb.append(" <a href=system/queryUser?page=" + i + " " + ( i == pager.getCurrentPage() ? "style=\"color:red;\"":"") + "> " + i + " </a>");	
				if(i==pageCount){
					break;
				}
			}
		}		
		sb.append("</div></td>");
		if(pager.getCurrentPage()-pageCount<0){
			sb.append("<td><div class='nextpage wordblue'>");
			sb.append("<a href=system/queryUser?page=" + ( pager.getCurrentPage() + 1 ) + ">下一页</a></div></td>");
			sb.append("<td><div class='wordblue'><a href=system/queryUser?page=" + pageCount + ">尾页</a></div></td>");
			}
		sb.append("</tr></table>");
		return sb.toString();
	}
	
	public void setAdmininfoMapper(AdmininfoMapper admininfoMapper) {
		this.admininfoMapper = admininfoMapper;
	}

	public List<Admininfo> getAdmininfos() {
		return admininfos;
	}
	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

}
